package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.RoleDto;
import com.itheima.ydd.entity.Role;
import com.itheima.ydd.entity.RoleMenu;
import com.itheima.ydd.service.RoleMenuService;
import com.itheima.ydd.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : xyiaao
 * @createDate : 2022/2/26 16:16
 */
@RestController
@RequestMapping("/sys_role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMenuService roleMenuService;

    /**
     * 权限管理 - 角色管理
     * 角色列表 - 分页
     *
     * @param page
     * @param limit
     * @param name
     * @return
     */
    @GetMapping("/list")
    public ResultData list(Integer page, Integer limit, String name) {

        Page<Role> pageInfo = new Page<>(page, limit);
        {
            // 分页查询角色信息
            LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(StringUtils.isNotEmpty(name), Role::getName, name);
            roleService.page(pageInfo, queryWrapper);
        }
        Page<RoleDto> roleDtoPage = new Page<>();
        // 拷贝数据
        BeanUtils.copyProperties(pageInfo, roleDtoPage, "records");

        // 对角色数据进行重新封装
        List<RoleDto> roleDtoList = pageInfo.getRecords().stream().map(role -> {
            RoleDto roleDto = new RoleDto();
            BeanUtils.copyProperties(role, roleDto);
            roleDto.setRoleId(role.getId());

            // 查询角色对应的菜单
            LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RoleMenu::getRoleId, role.getId());
            List<RoleMenu> roleMenuList = roleMenuService.list(queryWrapper);
            if (roleMenuList.isEmpty()) {
                return roleDto;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < roleMenuList.size(); i++) {
                RoleMenu roleMenu = roleMenuList.get(i);
                sb.append(roleMenu.getMenuId()).append(i == roleMenuList.size() - 1 ? "" : ",");
            }
            roleDto.setRoleMenus(sb.toString());
            return roleDto;
        }).collect(Collectors.toList());
        roleDtoPage.setRecords(roleDtoList);
        return ResultData.ok(PageResult.init(roleDtoPage));
    }


    /**
     * 权限管理 - 角色管理
     * 角色修改
     *
     * @param roleDto
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody RoleDto roleDto) {

        roleService.updateById(roleDto);
        roleMenuService.updateRoleMenu(roleDto);
        return ResultData.ok("修改成功");
    }


    /**
     * 权限管理 - 角色管理
     * 角色详情
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(String id) {

        Role role = roleService.getById(id);
        RoleDto roleDto = new RoleDto();
        BeanUtils.copyProperties(role, roleDto);
        // 查询角色所包含菜单并存入角色
        roleService.authList(roleDto);
        return ResultData.ok(roleDto);
    }


    /**
     * 权限管理 - 角色管理
     * 角色列表 - 全部
     *
     * @return
     */
    @GetMapping("/allData")
    public ResultData allData() {

        return ResultData.ok(roleService.list());

    }


    /**
     * 权限管理 - 角色管理
     * 角色添加
     *
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody RoleDto roleDto) {

        roleService.saveWithRoleMenu(roleDto.getName(), roleDto.getDescription(), roleDto.getMenuIds());
        return ResultData.ok("添加成功");
    }


    @GetMapping("/del")
    public ResultData del(String id) {

        roleService.removeWithRoleMenuIfNoUser(id);
        return ResultData.ok("删除成功");
    }
}
